package com.pe.pgn.clubpgn.webapp.controller.tipobono;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;

import com.pe.pgn.clubpgn.common.CLPConstantes;
import com.pe.pgn.clubpgn.domain.ClpdTipoBono;
import com.pe.pgn.clubpgn.service.TipoBonoService;
import com.pe.pgn.clubpgn.webapp.controller.BaseFormController;

@Controller
@RequestMapping("/tipobono/**")
public class TipoBonoController extends BaseFormController{

	@Autowired TipoBonoService tipoBonoService;
	
	@RequestMapping(value = "/tipobonolist.html")
    public void listarTiposBonos(Model model) {

		List<ClpdTipoBono> listTipoBono = tipoBonoService.listarTiposBono(false);
		model.addAttribute("listTipoBono", listTipoBono);
    }
	
	@RequestMapping(value = "/tipobonoform.html", method = RequestMethod.GET)
	public @ModelAttribute("tipobonoform") ClpdTipoBono verTipoBono(
		   @RequestParam(value = "id", required = false) Long id, Model model) {

		ClpdTipoBono bean  = new ClpdTipoBono();
		if (id != null) { 
			bean = tipoBonoService.obtenerTipoBono(id);
		}else{
			bean.setStTipoBono(true);
		}
		
		model.addAttribute("tipobonoform" , bean);
		return bean;
	}
	
	@RequestMapping(value = "/tipobonoform.html", method = RequestMethod.POST)
	public String guardarTipoBono(@ModelAttribute("tipobonoform") ClpdTipoBono tipoBono,
			BindingResult result, SessionStatus status, HttpServletRequest request, Model model) {

		try {

			if (result.hasErrors()) {
				model.addAttribute("tipobonoform", tipoBono);
				return "/tipobono/tipobonoform";
			}
			
			Map<String, Object> mapMsg = tipoBonoService.guardarTipoBono(tipoBono);
			if(mapMsg.get(CLPConstantes.MSG).equals(CLPConstantes.SUCCESS)){
				saveMessage(request, getText("common.added", request.getLocale()));
				return "redirect:tipobonolist.html";
			}else{
				
				tipoBono.setStTipoBono(true);
				if(mapMsg.get("detalle").equals("promocion_usa_tipobono")){
					saveError(request, getText("tipobonoform.validacion.deshabilito", request.getLocale()));
					return "/tipobono/tipobonoform";
				}else if(mapMsg.get("detalle").equals("bonoclientepersona_usa_tipobono")){
					saveError(request, getText("tipobonoform.validacion.deshabilito.bono", request.getLocale()));
					return "/tipobono/tipobonoform";
				}else if(mapMsg.get("detalle").equals("duplicidad_monto")){
					saveError(request, getText("tipobonoform.duplicidad.monto", request.getLocale()));
					return "/tipobono/tipobonoform";
				}else if(mapMsg.get("detalle").equals("duplicidad_descripcion")){
					saveError(request, getText("tipobonoform.duplicidad.desc", request.getLocale()));
					return "/tipobono/tipobonoform";
				}else{
					saveError(request, getText("errors.general", request.getLocale()));
					return "/tipobono/tipobonoform";
				}				
			}		
			
		} catch (Exception e) {
			saveError(request, getText("errors.general", request.getLocale()));
			return "/tipobono/tipobonoform";
		}
	}
	
	@RequestMapping(value = "/eliminartipobono.html")
	public String eliminartipobono(@RequestParam(value = "id", required = false) Long id, 
			HttpServletRequest request, Model model) {

		try {

			Map<String,Object> map = tipoBonoService.eliminarTipoBono(id);
			int rowsDeleted = map.get("rowsDeleted") != null ? Integer.parseInt(map.get("rowsDeleted").toString()) : 0;
			
			if(rowsDeleted > 0){
				saveMessage(request, getText("common.deleted", request.getLocale()));
				return "redirect:tipobonolist.html";
			}else{
				
				String detalle = String.valueOf(map.get("detalle"));				
				if(detalle.equals("clpb_promocion_bono")){
					saveError(request, getText("tipobonoform.validacion.eliminar", request.getLocale()));
					return "redirect:tipobonoform.html?id="+id;	
				}else if(detalle.equals("clpm_bono_cliente_persona")){
					saveError(request, getText("tipobonoform.validacion.eliminar.bono", request.getLocale()));
					return "redirect:tipobonoform.html?id="+id;	
				}else{
					saveError(request, getText("errors.general", request.getLocale()));
					return "redirect:tipobonoform.html?id="+id;
				}					
			}		
			
		} catch (Exception e) {
			saveError(request, getText("errors.general", request.getLocale()));
			return "redirect:tipobonoform.html?id="+id;
		}
	}
}
